#include "characterclassesmodel.h"


#include <dtwentymodel.h>
#include <characterclass.h>
#include <characterfeature.h>
#include <skill.h>
#include <classlevel.h>


#include <QMetaType>
#include <QMetaObject>
#include <QMetaEnum>
#include <QApplication>
#include <QFont>
#include <QBrush>
#include <QLinearGradient>

CharacterClassesModel::CharacterClassesModel(QObject *parent) :
    DTwentyModel(parent)
{
    m_titleTypeIds << qMetaTypeId<CharacterFeatures*>();
    m_titleTypeIds << qMetaTypeId<Skills*>();
    m_titleTypeIds << qMetaTypeId<ClassLevels*>();
}

QVariant CharacterClassesModel::data(const QModelIndex &index, int role) const
{
    if (m_root.isNull())
    {
        return QVariant();
    }

    QModelIndex pIndex = index.parent();
    int typeId = pIndex.data(DTwentyModel::Role_TYPE).toInt();

    if (typeId == qMetaTypeId<CharacterFeatures*>())
    {
        if (index.column() == 0 || role != Qt::DisplayRole)
        {
            return DTwentyModel::data(index, role);
        }

        if (index.column() != 2)
        {
            return QVariant(); // hide feature id
        }

        QVariant value = DTwentyModel::data(index, role);

        int offset = CharacterFeature::staticMetaObject.enumeratorOffset();
        QMetaEnum me;

        switch (index.row())
        {
        case 0:
             me = CharacterFeature::staticMetaObject.enumerator(offset);
             break;

        case 1:
            me = CharacterFeature::staticMetaObject.enumerator(offset+1);
            return QString("%1 (%2)").arg(me.valueToKey(value.toInt())).arg(value.toInt());

        case 2:
            me = CharacterFeature::staticMetaObject.enumerator(offset+2);
            break;

        case 3:
        case 4:
        case 5:
            me = CharacterFeature::staticMetaObject.enumerator(offset+3);
            break;

        default:
            return DTwentyModel::data(index, role);
        }

        return me.valueToKey(value.toInt());
    }

    if (role != Qt::FontRole && role != Qt::ForegroundRole)
    {
        return DTwentyModel::data(index, role);
    }

    typeId = index.data(DTwentyModel::Role_TYPE).toInt();
    if (!m_titleTypeIds.contains(typeId))
    {
        return DTwentyModel::data(index, role);
    }

    switch (role)
    {
    case Qt::FontRole:
    {
        QFont font = QApplication::font();

        if (index.column() == 0)
        {
            font.setPixelSize(14);
        }
        font.setBold(true);

        return font;
    }
    case Qt::ForegroundRole:
    {
        QBrush brush;
        brush.setColor(QColor("steelblue"));
        return brush;
    }
    default:
        break;
    }

    return DTwentyModel::data(index, role);
}

CharacterClass* CharacterClassesModel::characterClass() const
{
    return qobject_cast<CharacterClass*>(root());
}

void CharacterClassesModel::revert()
{
//    beginResetModel();
//    endResetModel();
}




